<template>
	<view>
		<view v-for="(item, index) in list" :key="index">
			<view class="cu-form-group">
				<view class="title">项目{{index+1}}：</view>
				<input placeholder="请输入(请不多于5个字)" v-model="item.name"></input>
			</view>
		</view>
		<view style="margin-top: 100upx;" @tap="toSave">
			<button style="width: 400upx;" class="cf-bgcolorTheme">保存</button>
		</view>
		<ayAd :showOffAt="true"></ayAd>
	</view>
</template>

<script>
	import ayAd from '@/components/ay-ad/ay-ad.vue';
	import comm from '@/api/comm.js'
	import preDupliClick from '@/api/preDupliClick.js'
	import storage from '@/store/storage.js'
	export default {
		components: {
			ayAd,
		},
		watch: {

		},
		data() {
			return {
				list: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
			};
		},
		onLoad: function() {
			let that = this;
		},
		onShow: function() {
			let that = this;
			that.setShowData();
		},
		methods: {
			setShowData() {
				let that = this;
				uni.getStorage({
					key: storage.storageKeyType.gift_list,
					success: function(res) {
						let data = res.data;
						console.log('读到礼物缓存')
						that.list = data;
					}
				});

			},

			async toSave() {
				let that = this;

				let isonce = preDupliClick.setpreDupliClickVal(preDupliClick.preDupli.one);
				if (!isonce) return;

				let iscan = false;
				let list = that.list;
				for (let i = 0; i < list.length; i++) {
					let index = i;
					let item = list[i];

					let name = item.name || '';
					if (name.trim().length == 0) {
						that.$api.msg('请输入项目' + (index + 1) + '！');
						iscan = false;
						break;
					}

					if (!comm.checkStrLenByNum(name)) {
						that.$api.msg('项目' + (index + 1) + '字数过长！');
						iscan = false;
						break;
					}
					
					iscan = true;
					
				}

				if (iscan) {
					let res_home_z = await that.$api.json('res_home');
					let gift_list = res_home_z.data.gift_list.data;

					gift_list.forEach(item => {
						let index = gift_list.indexOf(item);
						gift_list[index].name = list[index].name;
					})

					uni.setStorage({
						key: storage.storageKeyType.gift_list,
						data: gift_list,
						success: function() {
							that.$api.msg('保存成功！')
							uni.navigateBack({
							    delta: 1
							});
						}
					});
				}


			},
		},
	}
</script>

<style>
</style>
